﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Tayninh2011
{
    public sealed partial class FrmInit : RootForm
    {
        private FrmLogin frmLogin;// = FrmLogin.CallMe;
        private Form5 frm5;
        private FrmCash frmCash;// = FrmCash.CallMe;
        private FrmTableNames frmTableName;// = FrmTableNames.CallMe;
        private FrmAddSdeData frmAddSdeData;
        private FrmStart frmStart;
        //private static List<List<String>> lstLayers = new List<List<string>>();
        //private List<String[]> defaultLayers = new List<string[]>();
        private ITnUtilitiesFile fileUtil = new TnUtilities();
        private ITnUtilities util = new TnUtilities();
        

        private static readonly FrmInit frmInit = new FrmInit();
        DateTime now;
        #region Singleton
        private FrmInit()
        {
            InitializeComponent();
        }
        public static FrmInit CallMe
        {
            get { return frmInit; }
        }

        public new void Show()
        {
            if (isShown)
            {
                base.Show();
            }
            else
            {
                base.Show();
                
                isShown = true;
            }
        }
        private static bool isShown = false;

        static FrmInit()
        {

            frmInit.FormClosing += new FormClosingEventHandler(frm_FormClosing);

        }
        private static void frm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            isShown = false;
            frmInit.Hide();
        }
        #endregion Singleton

        //public List<List<String>> LstLayers
        //{
        //    get { return lstLayers; }
        //    set { lstLayers = value; }
        //}

        private void FrmInit_Load(object sender, EventArgs e)
        {
            //Thread.Sleep(3000);
            //MessageBox.Show("a");
            now = DateTime.Now;
            startService();
            frmStart = FrmStart.CallMe;
            frmLogin = FrmLogin.CallMe;
            frmCash = FrmCash.CallMe;
            frmTableName = FrmTableNames.CallMe;
            frmAddSdeData = FrmAddSdeData.CallMe;
            readLayersFile();
            //frmStart.Show();
            //this.Close();
            //frmLogin.Show();
            
            //this.backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            //MessageBox.Show("a");
            //try
            //{
                //frm5 = new Form5();
                //frm5.Show();
            startService();
                frmLogin = FrmLogin.CallMe;
                
            //}
            //catch (Exception e1) { MessageBox.Show(e1.Message); }
            //MessageBox.Show("b");
            //frmCash = FrmCash.CallMe;
            //frmTableName = FrmTableNames.CallMe;
            //frmAddSdeData = FrmAddSdeData.CallMe;
            frmTableName.Show();
            frmAddSdeData.Show();
            //readLayersFile();
            //sendInfo();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //DateTime next = DateTime.Now;
            //while (next.Second - now.Second < 3)
            //{
            //    next = DateTime.Now;
            //}
            //startService();
            this.Close();
            //this.frmLogin.Show();
        }
        private void readLayersFile()
        {
            try
            {
                if (System.IO.File.Exists(RootForm._strXmlLayersFileName + ".xml"))
                {
                    initLstLayers();
                    RootForm._lstLayersClf = fileUtil.ReadLayersFormXmlFile(RootForm._strXmlLayersFileName, "");
                    RootForm._lstLayers = util.GetLstOfLayers(RootForm._lstLayersClf);
                }
                else
                {
                    initLstLayers();
                    RootForm._lstLayers = RootForm._lstDefaultLayers;
                    fileUtil.CreateXmlFile("sde", RootForm._lstDefaultLayers, RootForm._strXmlLayersFileName, "");
                    RootForm._lstLayersClf = fileUtil.ReadLayersFormXmlFile(RootForm._strXmlLayersFileName, "");
                }
            }
            catch (Exception e) { MessageBox.Show(e.Message); }
        }
        private void initLstLayers()
        {
            RootForm._lstDefaultLayers.Add(new string[] { "thua", _strNameTableThua });
            RootForm._lstDefaultLayers.Add(new string[] { "duong", _strNameTableDuong });
            RootForm._lstDefaultLayers.Add(new string[] { "hem", _strNameTableHem });
            RootForm._lstDefaultLayers.Add(new string[] { "thua_giadat", _strNameTableThuaGiadat });
            //RootForm._lstLayers = _lstDefaultLayers;
        }
        //private void sendInfo()
        //{
        //    frmTableName.LstParams = this.LstLayers;
        //}
        private void startService()
        {
            //TnUtilities utilities = new TnUtilities();
            TnUtilities.StartService("esri_sde", 3000);
        }

        private void lblHover(object sender, EventArgs e)
        {
            ((Label)sender).Font = new Font(((Label)sender).Font, FontStyle.Underline);
            ((Label)sender).ForeColor = Color.Red;
        }

        private void lblLeave(object sender, EventArgs e)
        {
            ((Label)sender).Font = new Font(((Label)sender).Font, FontStyle.Regular);
            ((Label)sender).ForeColor = Color.DarkBlue;
        }

        private void lblLogin_MouseHover(object sender, EventArgs e)
        {
            lblHover(sender, e);
            lblDescription.Text = "Cần phải có cở sở dữ liệu SDE";
        }

        private void lblLogin_MouseLeave(object sender, EventArgs e)
        {
            lblLeave(sender, e);
            lblDescription.Text = String.Empty;
        }

        private void lblSeeOnly_MouseHover(object sender, EventArgs e)
        {
            lblHover(sender, e);
            lblDescription.Text = "Chỉ xem qua giao diện của phần mềm, \n không thực hiện chức năng tính toán. \n (Sử dụng khi không có cơ sở dữ liệu SDE.)";
        }

        private void lblSeeOnly_MouseLeave(object sender, EventArgs e)
        {
            lblLeave(sender, e);
            lblDescription.Text = String.Empty;
        }

        private void lblLogin_Click(object sender, EventArgs e)
        {
            frmLogin.ShowDialog();
            this.Close();
        }

        private void lblClose_MouseHover(object sender, EventArgs e)
        {
            lblHover(sender, e);
            //((Label)sender).BackColor = Color.LightBlue;
        }

        private void lblClose_MouseLeave(object sender, EventArgs e)
        {
            lblLeave(sender, e);
            //((Label)sender).BackColor = Color.White;
        }

        private void lblClose_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
            Application.Exit();
        }

        private void lblSeeOnly_Click(object sender, EventArgs e)
        {
            modeApp = TnModeApp.SEE_ONLY;
            IsSeeOnly = true;
            FrmTnMain frm6 = FrmTnMain.CallMe;
            frm6.Show();
            
        }

        
    }
}
